package algorithm.queue;

import lombok.Data;

import java.util.Scanner;

/**
 * @ClassName : data_structure.queue.ArrayQueueDemo
 * @Description : 类描述
 * Created by cyd on 2021-01-19 21:55:36
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while (loop) {
            System.out.println("----------------------");
            System.out.println("e：退出程序");
            System.out.println("s：显示队列");
            System.out.println("a：添加元素");
            System.out.println("g：取出元素");
            System.out.println("h：查看队列头");

            System.out.println("请输入指令：");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("----------------------");
                    System.out.println("请输入一个数：");
                    Integer value = scanner.nextInt();
                    try {
                        queue.addElement(value);

                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        Integer res = queue.getElement();
                        System.out.printf("取出来的数据是：%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        Integer res = queue.peek();
                        System.out.printf("取出来的数据是：%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
            }
        }
        System.out.println("程序已结束！");
    }
}

@Data
class ArrayQueue {
    // 队列最大容量
    private Integer maxSize;
    // 头指针
    private Integer front;
    // 尾指针
    private Integer rear;
    // 队列容器
    private Integer[] arr;


    // 队列的构造器
    public ArrayQueue(Integer maxSize) {
        this.maxSize = maxSize;
        arr = new Integer[maxSize];
        // 队列初始化时，头尾指针指向队列列头的前一个位置
        front = -1;
        rear = -1;
    }


    // 判断队列是否已满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return front.equals(rear);
    }

    // 队列添加元素
    public void addElement(Integer ele) {
        if (isFull()) {
            throw new RuntimeException("队列已满，添加失败");
        }

        rear++;
        arr[rear] = ele;
    }

    // 队列剔除
    public Integer getElement() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空，取出失败");
        }

        front++;
        Integer result = arr[front];
        // 此处可以置空
        arr[front] = null;
        return result;
    }

    // 显示整个队列
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列为空");
            return;
        }

        for (int i = (front + 1); i < (rear + 1); i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }

    }

    // 查看队列头
    public Integer peek() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }

        return arr[front + 1];
    }
}
